package com.lc.afbx.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.afbx.BaseApplication;
import com.lc.afbx.R;
import com.lc.afbx.conn.GetApiPersonalCenter;
import com.lc.afbx.conn.PostHeadImg;
import com.lc.afbx.conn.UpdateAppGet;
import com.lc.afbx.dialog.AlertDialog;
import com.lc.afbx.dialog.MyAlertDialog;
import com.lc.afbx.dialog.PictureDialog;
import com.lc.afbx.util.MyToast;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;
import com.zcx.helper.util.UtilBase64;
import com.zcx.helper.util.UtilLog;
import com.zcx.helper.util.UtilToast;

import cn.trinea.android.common.util.ToastUtils;

/**
 * Created by Administrator on 2016/5/19.
 */
public class PersonalCenterActivity extends BasePictureActivity implements View.OnClickListener {


    private RelativeLayout rLayoutXiangXiXinXi;
    private LinearLayout rLayoutWeiDengLu;
    private TextView userName;
    private TextView level;
    private Button btnLogOut;

    private TextView personal_center_login_tv;
    private TextView personal_center_regist_tv;
    private RelativeLayout qr_code_rl;

    private boolean isLogin;
    private String path;

    private SimpleDraweeView headImg;

    private UpdateAppGet updateAppGet = new UpdateAppGet(new AsyCallBack<UpdateAppGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, final UpdateAppGet.Info info) throws Exception {

            if (info.number > UtilApp.getPackageInfo(context).versionCode) {

                new MyAlertDialog(context, "有新版本啦", "去更新") {
                    @Override
                    protected void onConfirm() {

                        Uri uri = Uri.parse(info.link);

                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);

                        startActivity(intent);
                    }
                }.show();


            } else {

                MyToast.showToast("已经是最新版本", context);
            }

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            Log.e("----", updateAppGet + "失败了");
        }
    });

    //上传头像接口
    private PostHeadImg postHeadImg = new PostHeadImg(new AsyCallBack<PostHeadImg.Info>() {
        @Override
        public void onSuccess(String toast, int type, PostHeadImg.Info info) throws Exception {

            UtilToast.show(context, toast);

            //图片裁剪小格式 显示用
            headImg.setImageURI(Uri.parse("file://" + path));

            UtilLog.e("头像地址：", "file://" + path);

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);

        }
    });
    //获取个人信息接口
    private GetApiPersonalCenter getApiPersonalCenter = new GetApiPersonalCenter(new AsyCallBack<GetApiPersonalCenter.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetApiPersonalCenter.Info info) throws Exception {
//            String str = info.data.username.trim();
//            str.replace(str.substring(3, 7), "****");

            userName.setText(info.data.username);
            level.setText(info.data.level);

            headImg.setImageURI(Uri.parse(info.data.headurl));
        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            UtilToast.show(context, toast);
        }
    });




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_geren);


        qr_code_rl = ((RelativeLayout) findViewById(R.id.qr_code_rl));

        rLayoutXiangXiXinXi = ((RelativeLayout) findViewById(R.id.geren_xiangxixinxi));
        //未登录
        rLayoutWeiDengLu = ((LinearLayout) findViewById(R.id.geren_weidenglu));
        //用户名
        userName = ((TextView) findViewById(R.id.geren_yonghuming));
        //等级
        level = ((TextView) findViewById(R.id.geren_dengji));
        //头像
        headImg = ((SimpleDraweeView) findViewById(R.id.geren_touxiang));
        //退出按钮
        btnLogOut = ((Button) findViewById(R.id.btn_logout));
        //登录注册 按钮
        personal_center_login_tv = (TextView) findViewById(R.id.personal_center_login_tv);
        personal_center_regist_tv = (TextView) findViewById(R.id.personal_center_regist_tv);

        //退出登录
        btnLogOut.setOnClickListener(this);
        headImg.setOnClickListener(this);
        personal_center_login_tv.setOnClickListener(this);
        personal_center_regist_tv.setOnClickListener(this);
        qr_code_rl.setOnClickListener(this);

        setAppCallBack(new geRenZhongXinCallBack());

        new geRenZhongXinCallBack().refresh();


    }


    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.geren_back:
                finish();
                return;

            case R.id.geren_xiangxixinxi:

                startActivity(new Intent(this, PersonalInfoActivity.class));
                break;

            case R.id.geren_baodan:
                if (isLogin) {

                    startActivity(new Intent(this, PolicyActivity.class));

                } else {
                    personal_center_login_tv.performClick();
                    return;
                }
                break;

            case R.id.geren_yongjin:
                if (isLogin) {

                    startActivity(new Intent(this, CommissionActivity.class));

                } else {
                    personal_center_login_tv.performClick();
                    return;
                }
                break;

            case R.id.geren_tuandui:

                if (isLogin) {

                    if (level.getText().toString().equals("会员")) {

                        MyToast.showToast("会员级别不够", context);
                        return;
                    }

                    Bundle bundle = new Bundle();
                    startActivity(new Intent(this, TeamActivity.class).putExtras(bundle));

                } else {
                    personal_center_login_tv.performClick();
                    return;
                }

                break;

            case R.id.qr_code_rl:

                startActivity(new Intent(this, QrCodeActivity.class));

                break;

            case R.id.geren_guanyuwomen:

                startActivity(new Intent(this, AboutUsActivity.class));

                break;

            case R.id.geren_baoxianjifen:
                if (isLogin) {

                    startActivity(new Intent(this, IntegralActivity.class));

                } else {
                    personal_center_login_tv.performClick();
                    return;
                }
                break;

            case R.id.geren_yijianfankui:
                if (isLogin) {

                    startActivity(new Intent(this, FeedbackActivity.class));

                } else {
                    personal_center_login_tv.performClick();
                    return;
                }
                break;

            case R.id.geren_changjianwenti:

                startActivity(new Intent(this, ProblemsActivity.class));

                break;

            case R.id.geren_lianxikefu:

                startActivity(new Intent(this, CallServiceActivity.class));

                break;

            case R.id.personal_center_login_tv:

                startActivity(new Intent(this, LoginActivity.class));

                break;

            case R.id.personal_center_regist_tv:

                startActivity(new Intent(this, RegistActivity.class));

                break;

            case R.id.geren_banbengengxin:

                updateAppGet.execute(this);

                return;

            case R.id.btn_logout:

                new AlertDialog(this, "是否退出登录") {
                    @Override
                    protected void onConfirm() {

                        BaseApplication.BasePreferences.saveIsSetPay(false);
                        BaseApplication.BasePreferences.saveIsLogin(false);
                        BaseApplication.BasePreferences.saveCityID("");
                        BaseApplication.BasePreferences.saveUserName("");
                        BaseApplication.BasePreferences.saveUserId("");
                        BaseApplication.BasePreferences.saveCode("");
                        new geRenZhongXinCallBack().refresh();
                    }

                    @Override
                    protected void onCancle() {

                    }
                }.show();

                return;

            case R.id.geren_touxiang:

                new PictureDialog(context) {
                    @Override
                    protected void onCamera() {

                        startCamera(null);

                    }

                    @Override
                    protected void onAlbum() {

                        startAlbum(null);

                    }
                }.show();

                return;

        }


    }

    @Override
    protected Crop getCrop() {
        return new Crop().setCrop(true);
    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {

        path = s;

        postHeadImg.userid = BaseApplication.BasePreferences.readUserId();
        //转成base64格式 上传
//        postHeadImg.avatar = PictureUtil.bitmapToString(s);

        postHeadImg.avatar = UtilBase64.encodeBase64File(s);

        postHeadImg.execute(context);

    }

    public class geRenZhongXinCallBack implements AppCallBack {

        public void refresh() {

            //清空后取出来的 就是空的
            isLogin = BaseApplication.BasePreferences.readIsLogin();

            if (isLogin) {
                //隐藏登录按钮 ，显示退出按钮
                rLayoutXiangXiXinXi.setVisibility(View.VISIBLE);
                rLayoutWeiDengLu.setVisibility(View.GONE);
                btnLogOut.setVisibility(View.VISIBLE);

                getApiPersonalCenter.userid = BaseApplication.BasePreferences.readUserId();
                getApiPersonalCenter.execute(PersonalCenterActivity.this);
            } else {
                //隐藏退出按钮 ，显示登录按钮
                rLayoutXiangXiXinXi.setVisibility(View.GONE);
                rLayoutWeiDengLu.setVisibility(View.VISIBLE);
                btnLogOut.setVisibility(View.GONE);

            }

        }

    }

}
